//
//  AppDelegate.swift
//  DaRings
//
//  Created by 尹琼 on 2022/10/27.
//

import UIKit
import Hue
import SwiftDate
import MYModule

import SQLite
import FirebaseCore
import Bugly

#if DEBUG

import FLEX

#endif

@main

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    var isForceLandscape:Bool = false
    var isForcePortrait:Bool = false
    var isForceAllDerictions:Bool = false //支持所有方向

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        
        self.crashToolConfig()
        
        // 设置默认 region
        SwiftDate.defaultRegion = Region(calendar: Calendar.autoupdatingCurrent, zone: Zones.current, locale: Locales.current)
        
        self.sqliteConfig()
        
        self.initBluetoothManager()
        
        self.updateCongfig()
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        
        let rootVc = TabBarProvider.lottieSytle()
        window?.rootViewController = rootVc
        window?.makeKeyAndVisible()
        
        /// 判断是否是首次启动
        firstLanch()
        
        /// 权限
        permission()
        
        self.showFlex()
        
        return true
    }
    
    
    private func crashToolConfig() {
        
        guard let AppName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String else {
            return
        }
        
        if AppName == "Da Rings" {
            // fireBase
            FirebaseApp.configure()
        } else {
            
            // Buly
            Bugly.start(withAppId: "d99b2820b2")
            
        }
        
        
    }
    
    private func updateCongfig() {
        
        let manager = BandConfigManager.shared
        
        if BluetoothManager.shared.deviceState == .connected {
            return
        }
        
        manager.updateConfig()
        
    }
    
    private func initBluetoothManager() {
        
        // 添加所有的ble数据通知接收
        BluetoothResponder.shared.addNotifacations()

        BluetoothManager.shared.setBluetoothConfig()
        BluetoothManager.shared.setTime()
    }
    
    /// 通知
    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
       
        guard let userInfo = notification.userInfo else {
            return
        }
        //处理通知
        
        
    }
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if self.isForceAllDerictions {
            return .all
        } else if self.isForceLandscape {
            return .landscape
        } else if self.isForcePortrait {
            return .portrait
        }
        return .portrait
    }
}


// MARK: - SQLite Manager
extension AppDelegate: SQLiteManagerDelegate {
    
    func createTables(_ manager: SQLiteManager) -> [SQLiteTableiabe.Type] {
        
        return [
            
            HeartRateModel.self,
            SingleHeartRateModel.self,
            BloodOxygenModel.self,
            SingleBloodOxygenModel.self,
            SleepModel.self,
            AmountActivityModel.self,
            StepArchiveModel.self,
            TemperatureModel.self,
            HRVModel.self,
            SingleHRVModel.self,
            BandConfigModel.self,
            TrainingModel.self,
            ActivityModel.self,
            SingleStressModel.self,
            ExerciseGoalsModel.self,
        ]
    }
    
    func didCreateTables(_ manager: SQLiteManager) {}
    
    func sqliteConfig() {
        
        SQLiteManager.shared.delegate = self
        SQLiteManager.shared.makeTables()
    }
    
}

extension AppDelegate {
    
    
    
    func firstLanch() {
        
        if ProfileController.lanched == false {
            let vc = ProfileController()
            vc.isSetProfile = true
            window?.rootViewController?.present(vc, animated: false)
        }
        
    }
    
    func permission() {
        
        /// 通知权限
        Permissioner.userNotification()
        
    }
    
}

extension AppDelegate {
    
    func showFlex() {
        
        #if DEBUG
        
        FLEXManager.shared.showExplorer()
        
        #endif
    }
    
}


